package com.lz.spring.cloud.ribbon.control;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;


@RestController
public class CalcControl {

	@Autowired
	private RestTemplate restTemplate;
	
    @Autowired
    private LoadBalancerClient loadBalancer;
    
	@GetMapping("/add/{a}/{b}")
	@HystrixCommand(fallbackMethod = "addFallback")
	public String add(@PathVariable Integer a, @PathVariable Integer b){
		ServiceInstance instance = this.loadBalancer.choose("03-spring-eureka-client");
		System.out.println("====" + instance.getServiceId() + ":" + instance.getHost() + ":" + instance.getPort());
		return restTemplate.getForObject("http://03-spring-eureka-client/add/"+ a + "/" + b, String.class);
	}
	
	public String addFallback(Integer a, Integer b){
		System.out.println("===进入了addFallback===");
		return "进入了addFallback";
	}
}
